//
//  homeViewController.m
//  tangDynasty
//
//  Created by 洪闽程 on 2018/11/15.
//  Copyright © 2018年 洪闽程. All rights reserved.
//

#import "homeViewController.h"
#import "hmcLastCollectionViewCell.h"
#import "dataModel.h"
#import "MDHttpService.h"
#import <MJExtension.h>
#import "videoViewController.h"
#import "MBProgressHUD+MD.h"
#import <MJRefresh.h>
#import "MDObjManager.h"
@interface homeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic,strong)NSArray<dataModel *> * dataArr;

/**<##>*/
@property (nonatomic,assign)NSInteger number;

@end

@implementation homeViewController
-(void)viewDidAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _number=6;
    
    [self loadData];

    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    self.navigationItem.title=@"首页";
    
    self.collectionView.showsVerticalScrollIndicator=NO;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing=15;
    layout.minimumInteritemSpacing=0;
    [_collectionView setCollectionViewLayout:layout];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"hmcLastCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    _collectionView.contentInset=UIEdgeInsetsMake(10, 0, 10, 0);
    
  

    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _number+=4;
  
        if (_number>self.dataArr.count)
        {
            
             [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
       else
       {
             [self.collectionView reloadData];
           
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            [self.collectionView.mj_footer endRefreshing];
        });
       }
    }];
    
}

-(void)loadData
{
    
    
    
//    if ([NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"homeData.text"]])
//    {
//        self.dataArr=[NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"homeData.text"]];
//    }
//    else
//    {
    NSString *url=@"http://www.wuqiongda8888.com/app/list.htm";
    
    MDHttpService *httservice=[MDHttpService shareHttpService];
    
    
    NSDictionary *param=@{@"id":@"75",@"page":@"1",@"pageSize":@"100000",@"videoSize":@"100000"};
    
    [httservice postUrlStr:url andparam:param andsuccess:^(NSDictionary *dic) {

        _dataArr=[dataModel mj_objectArrayWithKeyValuesArray:dic[@"content"][@"categoryVideos"][0][@"videos"]];
     
        [NSKeyedArchiver archiveRootObject:_dataArr toFile:[MDObjManager KeyedArchiverPathName:@"homeData.text"]];
        
        [self.collectionView reloadData];
        
    } andfailed:^(MError *error)
     {
         [MBProgressHUD showError:@"网络连接失败"];
    }];
//    }
//    
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    hmcLastCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.datamodel=_dataArr[indexPath.row];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width*0.5-20,[UIScreen mainScreen].bounds.size.width*0.5);
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _number;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    videoViewController *videoVc=[[videoViewController alloc]init];
    
    videoVc.selectRow=indexPath.row;
    
    videoVc.model=_dataArr[indexPath.row];
    



    [self presentViewController:videoVc animated:YES completion:nil];
    
    
    
    
}



@end
